package com.lijunyan.blackmusic.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.lijunyan.blackmusic.R;
import com.lijunyan.blackmusic.database.DBUserInfoLogin;
import com.lijunyan.blackmusic.entity.UserInfo;
import com.lijunyan.blackmusic.eventbus.LoginStateEventBus;
import com.lijunyan.blackmusic.util.MyApplication;
import com.lijunyan.blackmusic.util.MyMusicUtil;
import com.lijunyan.blackmusic.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

/*
 * path: RestaurantDemo
 * Author: 86133
 * Create: 2019/12/3 11:35
 * Changes (from 2019/12/3)
 * Desc:
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back;

    private TextView title;

    private TextView register_user_name_next;

    private String userName;

    private String passWord;

    private String passWordAgain;

    private EditText register_user_name;

    private EditText register_user_password;

    private EditText register_user_password_again;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user_name_layout);
        prepare();
        initView();
        setListener();
    }

    private void prepare() {
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);

        title = (TextView) findViewById(R.id.title);
        title.setText("注册");
        title.setVisibility(View.VISIBLE);
        register_user_name = (EditText) findViewById(R.id.register_user_name);
        register_user_password = (EditText) findViewById(R.id.register_user_password);
        register_user_password_again = (EditText) findViewById(R.id.register_user_password_again);
        register_user_name_next = (TextView) findViewById(R.id.register_user_name_next);

        initTheme();
    }

    private void initTheme(){
        int themeId = MyMusicUtil.getTheme(RegisterActivity.this);
        switch (themeId){
            default:
            case 0:
                register_user_name_next.setBackgroundResource(R.color.biliPink);
                break;
            case 1:
                register_user_name_next.setBackgroundResource(R.color.zhihuBlue);
                break;
            case 2:
                register_user_name_next.setBackgroundResource(R.color.kuanGreen);
                break;
            case 3:
                register_user_name_next.setBackgroundResource(R.color.cloudRed);
                break;
            case 4:
                register_user_name_next.setBackgroundResource(R.color.tengluoPurple);
                break;
            case 5:
                register_user_name_next.setBackgroundResource(R.color.seaBlue);
                break;
            case 6:
                register_user_name_next.setBackgroundResource(R.color.grassGreen);
                break;
            case 7:
                register_user_name_next.setBackgroundResource(R.color.coffeeBrown);
                break;
            case 8:
                register_user_name_next.setBackgroundResource(R.color.lemonOrange);
                break;
            case 9:
                register_user_name_next.setBackgroundResource(R.color.startSkyGray);
                break;
            case 10:
                register_user_name_next.setBackgroundResource(R.color.nightActionbar);
                break;
        }
    }

    private void setListener() {
        back.setOnClickListener(this);
        register_user_name_next.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.register_user_name_next:
                postUserRegisterMessage();
                break;
        }
    }

    /**
     * @Desc:  发送用户注册信息
     * @Author:  86133
     * @Time:  2019/6/11 16:15
     */
    @SuppressLint("CheckResult")
    private void postUserRegisterMessage(){
        try {
            Map map = new HashMap();

            userName = register_user_name.getText().toString().trim();
            if(TextUtils.isEmpty(userName)){
                ToastUtil.showToast("请输入用户名");
                return;
            }else{
                map.put("username", userName);
            }
            passWord = register_user_password.getText().toString().trim();
            if(TextUtils.isEmpty(passWord)){
                ToastUtil.showToast("请输入密码");
                return;
            }else{
                map.put("password", passWord);
            }

            passWordAgain = register_user_password_again.getText().toString().trim();
            if(TextUtils.isEmpty(passWordAgain)){
                ToastUtil.showToast("请输入确认密码");
                return;
            }

            if(!passWordAgain.equals(passWord)){
                ToastUtil.showToast("两次密码输入不一致");
                return;
            }

            Boolean hadRegister = DBUserInfoLogin.queryUserInfo(this, userName);
            if(hadRegister){
                ToastUtil.showToast("用户已存在，请重新注册");
            }else{
                long l = DBUserInfoLogin.insertUserInfo(this, userName, passWord, System.currentTimeMillis() + "");
                if(l>0){
                    ToastUtil.showToast("注册成功");
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserName(userName);
                    userInfo.setUserPassword(passWord);
                    saveUserName(userName);
                    EventBus.getDefault().post(new LoginStateEventBus(true,userInfo));
                    finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Desc:  保存用户信息
     * @Author:  86133
     * @Time:
     */
    private void saveUserName(String userName) {
        SharedPreferences spf = MyApplication.getContext().getSharedPreferences("SP_LOGIN_NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        if(!TextUtils.isEmpty(userName)){
            editor.putString("userName", userName);
        }
        editor.commit();
    }

    /**
     * @param context
     */
    public static void startRegisterActivity(Context context) {
        if (context != null) {
            Intent intent = new Intent(context, RegisterActivity.class);
            context.startActivity(intent);
        }
    }
}
